import React, { Component } from "react";
import PropTypes from "prop-types";

import Pie from "../../components/LineChart/smartPie"
import Stack from "../../components/LineChart/smartStackWS"

export default class Chart extends Component {
    constructor(props) {
        super(props);
    }
    render() {
        const { chartData, type, key } = this.props || {};
        let stackData = [];
        let pieData = [];
        if(chartData.length > 3 || type === 'stack') {
            stackData = chartData.map(val => {
                return { xAxisData: val.key, yAxisData: val.value, name: val.key }
            });
        }else {
            pieData = chartData.map((val, index) => {
                return { a: '1', b: val.value, c: val.key }
            })
        }
        return (
            <div className="chart">
                {
                    chartData.length > 0 ?
                    (
                        chartData.length > 3 || type === 'stack' ?
                        <Stack 
                            chartData={stackData}
                            useTooltip={false}
                            key={key}
                        /> : 
                        <Pie
                            chartData={pieData}
                            useTooltip={false}
                            showLegend={true}
                        /> 
                    ) : null
                    
                }
            </div>
        );
    }
}
Chart.propTypes = {
    chartData: PropTypes.array
};